<template>
  <div class="login-container">
     
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import SocialSign from './components/SocialSignin'
import { code } from '@/api/user'
export default {
  name: 'logout',
  components: { SocialSign },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('手机号有误'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (!value||value.length ==0) {
        callback(new Error('验证码有误'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'admin',
        password: '',
        phone:'',
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
      show:true,
      count:60,
    }
  },
  // watch: {
  //   $route: {
  //     handler: function(route) {
  //       const query = route.query
  //       if (query) {
  //         this.redirect = query.redirect
  //         this.otherQuery = this.getOtherQuery(query)
  //       }
  //     },
  //     immediate: true
  //   }
  // },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted() {
    this.logout();
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
     logout(){
       this.$confirm('确认退出？')
          .then(_ => {
           this.logoutok();
          })
          .catch(_ => {});
    },
       async logoutok() {
       
         this.$loadingshow();
           await this.$store.dispatch('tagsView/delAllViews')
      await this.$store.dispatch('user/logout')
      // console.log('re:'+`/login?redirect=${this.$route.fullPath}`)
      this.$loadingclose();
      this.$router.push(`/login?redirect=${this.$route.fullPath}`)
    },
      navtype(){
	    	var typ='';
	    	var u = navigator.userAgent;
			if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {//安卓手机
				typ='a';
			} else if (u.indexOf('iPhone') > -1) {//苹果手机
				typ='i';
			} else if (u.indexOf('Windows Phone') > -1) {//winphone手机
				typ='w';
			}
			return typ;

},
     inputBlur() {
      if(this.navtype()=='i') {
        setTimeout(() => {
          document.body.scrollTop = 0;
        }, 200)
      }
    },

 
    checkCapslock({ shiftKey, key } = {}) {
      if (key && key.length === 1) {
        if (shiftKey && (key >= 'a' && key <= 'z') || !shiftKey && (key >= 'A' && key <= 'Z')) {
          this.capsTooltip = true
        } else {
          this.capsTooltip = false
        }
      }
      if (key === 'CapsLock' && this.capsTooltip === true) {
        this.capsTooltip = false
      }
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
            .then(() => {
              console.log('success ok login1')
              // alert('suu')
              // this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
              this.$router.push('/profile')
              this.loading = false
            })
            .catch(() => {
               console.log('success ok login2')
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
    // afterQRScan() {
    //   if (e.key === 'x-admin-oauth-code') {
    //     const code = getQueryObject(e.newValue)
    //     const codeMap = {
    //       wechat: 'code',
    //       tencent: 'code'
    //     }
    //     const type = codeMap[this.auth_type]
    //     const codeName = code[type]
    //     if (codeName) {
    //       this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
    //         this.$router.push({ path: this.redirect || '/' })
    //       })
    //     } else {
    //       alert('第三方登录失败')
    //     }
    //   }
    // }


,
      getcode() {
      if (!this.loginForm.phone) {
        // this.$toast("手机号码不能为空");
          this.$message({
        message: '手机号码不能为空',
        type: 'fail',
        duration: 5 * 1000
      })
        return;
      }
      // this.loginForm.phone=this.logindata.username;
      window.localStorage.setItem("phone", this.loginForm.phone);
      code({ phone: this.loginForm.phone, loginMethod: 2 }).then(res => {
        if (res.code == 200) {
          this.get();
          // this.$toast("短信发送成功");
            this.$message({
        message: '短信发送成功',
        type: 'success',
        duration: 5 * 1000
      })
        } else if (res.code == 150) {
     this.$message({
        message: res.message,
        type: 'fail',
        duration: 5 * 1000
      })
        } else if (res.code == 500) {
           this.$message({
        message: '短信发送过于频繁，一分钟后再试',
        type: 'fail',
        duration: 5 * 1000
      })
        }
      });
    },
      get() {
      const TIME_COUNT = 60;
      if (!this.timer) {
        this.count = TIME_COUNT;
        this.show = false;
        this.timer = setInterval(() => {
          if (this.count > 0 && this.count <= TIME_COUNT) {
            this.count--;
          } else {
            this.show = true;
            clearInterval(this.timer);
            this.timer = null;
          }
        }, 1000)
      }
    }
  }
}
</script>

<style lang="scss"  >
.validate{
      width: 35%;
    /* margin-bottom: 30px; */
    text-align: center;
    padding-left: 0px;
    padding-right: 0px;
    position: absolute;
    top: 7px;
    right: 7px;
}
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
    // width:57%;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    // width:57%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
